﻿using Comdiv.QWeb.Files;
using Comdiv.QWeb.Utils;

namespace Comdiv.QWeb.Renders {
	[Render("form")]
	public class FormRender : QViewRender {
		private static bool initialized;
		private static readonly object sync = new object();

		public override bool NeedResult {
			get { return false; }
		}

		private static void initialize(QWebContext context) {
			if (initialized) return;
			lock (sync) {
				context.Registry.FileNameResolver.Externalize("(actionform)|(qwebsystem)");
				initialized = true;
			}
		}

		protected override object getViewData(QWebContext context) {
			context.ActionDescriptor.SetupBinders();
			return context.ActionDescriptor;
		}

		protected override string getViewName(QWebContext context) {
			initialize(context);
			return "qwebsystem/actionform";
		}

		protected override string getLayout(QWebContext context) {
			var result = base.getLayout(context);
			if (result.hasContent()) return result;
			if (context.get("ajax", false)) return "";
			return "actionform";
		}
	}
}